package aiyh.utils.sqlUtil.whereUtil.impl;

import org.jetbrains.annotations.NotNull;
import aiyh.utils.sqlUtil.whereUtil.Where;
import aiyh.utils.sqlUtil.whereUtil.WhereOperator;
import weaver.conn.RecordSet;

import java.util.List;

/**
 * @author EBU7-dev1-ayh
 * @date 2021/8/23 0023 13:23
 */


public class PrepWhereOperatorImpl implements WhereOperator {
	private final PrepWhereImpl where;
	private String DB_TYPE;

	{
//		获取当前数据库的类型
		this.DB_TYPE = (new RecordSet()).getDBType();
	}

	public PrepWhereOperatorImpl(PrepWhereImpl where) {
		this.where = where;
	}


	@Override
	public Where whereNull() {
		this.where.whereAppend(" is null ");
		return this.where;
	}

	@Override
	public Where whereNotNull() {
		this.where.whereAnd(" is not null ");
		return this.where;
	}

	@Override
	public Where whereEqual(Object value) {
		this.where.whereAppend(" = ? ");
		this.where.addArgs(value);
		return this.where;
	}

	@Override
	public Where whereNotEqual(Object value) {
		this.where.whereAppend(" <> ? ");
		this.where.addArgs(value);
		return this.where;
	}

	@Override
	public Where greaterThan(Object value) {
		this.where.whereAppend(" > ? ");
		this.where.addArgs(value);
		return this.where;
	}

	@Override
	public Where lessThan(Object value) {
		this.where.whereAppend(" < ? ");
		this.where.addArgs(value);
		return this.where;
	}

	@Override
	public Where greaterOrEqual(Object value) {
		this.where.whereAppend(" >= ? ");
		this.where.addArgs(value);
		return this.where;
	}

	@Override
	public Where lessThanOrEqual(Object value) {
		this.where.whereAppend(" <= ? ");
		this.where.addArgs(value);
		return this.where;
	}

	@Override
	public Where BetweenAnd(Object start, Object end) {
		this.where.whereAppend(" between ? ");
		this.where.addArgs(start);
		this.where.whereAppend(" and ? ");
		this.where.addArgs(end);
		return this.where;
	}

	@Override
	public Where whereIn(Object... values) {
		this.where.whereAppend(" in ( ");
		return getWhere(values);
	}

	@NotNull
	private Where getWhere(Object[] values) {
		for (int i = 0; i < values.length; i++) {
			this.where.whereAppend(" ? ");
			this.where.addArgs(values[i]);
			if (i < values.length - 1) {
				this.where.whereAppend(",");
			}
		}
		this.where.whereAppend(" ) ");
		return this.where;
	}

	@Override
	public Where whereInList(List<Object> list) {
		this.where.whereAppend(" in ( ");
		for (int i = 0; i < list.size(); i++) {
			this.where.whereAppend(" ? ");
			this.where.addArgs(list.get(i));
			if (i < list.size() - 1) {
				this.where.whereAppend(",");
			}
		}
		this.where.whereAppend(" ) ");
		return this.where;
	}

	@Override
	public Where whereNotIn(Object ...values) {
		this.where.whereAppend(" not in ( ");
		return getWhere(values);
	}

	@Override
	public Where whereNotInList(List<Object> list) {
		this.where.whereAppend(" not in ( ");
		return getWhere(list);
	}

	@Override
	public Where inSql(Object sql) {
		this.where.whereAppend(" in ( ");
		this.where.whereAppend(sql);
		this.where.whereAppend(" ) ");
		return this.where;
	}

	@Override
	public Where notInSql(Object sql) {
		this.where.whereAppend(" not in ( ");
		this.where.whereAppend(sql);
		this.where.whereAppend(" ) ");
		return this.where;
	}

	@Override
	public Where whereLike(Object value) {
		this.where.whereAppend(" like ?");
		this.where.addArgs(value);
		return this.where;
	}

	@Override
	public Where whereNotLike(Object value) {
		this.where.whereAppend(" not like ?");
		this.where.addArgs(value);
		return this.where;
	}

	@Override
	public Where whereExists(Object ...values) {
		this.where.whereAppend(" exists ( ");
		return getWhere(values);
	}

	@Override
	public Where whereNotExists(Object ...values) {
		this.where.whereAppend(" not exists ( ");
		return getWhere(values);
	}

	@Override
	public Where whereExistsList(List<Object> list) {
		this.where.whereAppend(" exists ( ");
		return getWhere(list);
	}

	@Override
	public Where whereNotExistsList(List<Object> list) {
		this.where.whereAppend(" not exists ( ");
		return getWhere(list);
	}

	@NotNull
	private Where getWhere(List<Object> list) {
		for (int i = 0; i < list.size(); i++) {
			this.where.whereAppend(" ? ");
			this.where.addArgs(list.get(i));
			if (i < list.size() - 1) {
				this.where.whereAppend(", ");
			}
		}
		this.where.whereAppend(" ) ");
		return this.where;
	}

	@Override
	public Where whereExistsSql(Object sql) {
		this.where.whereAppend(" exists ( ");
		this.where.whereAppend(sql);
		this.where.whereAppend(" ) ");
		return this.where;
	}

	@Override
	public Where whereNotExistsSql(Object sql) {
		this.where.whereAppend(" not exists ( ");
		this.where.whereAppend(sql);
		this.where.whereAppend(" ) ");
		return this.where;
	}
}
